package com.share.device.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.domain.StationVo;
import com.share.device.service.ICabinetService;
import com.share.device.service.IDeviceService;
import com.share.device.service.IStationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DeviceServiceImpl implements IDeviceService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IStationService stationService;

    @Autowired
    private ICabinetService cabinetService;


    @Override
    public List<StationVo> nearbyStation(String latitude, String longitude) {
        //确定中心点
        //经度和维度
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.parseDouble(longitude), Double.parseDouble(latitude));

        //设置查询半径，比如查询50公里
        Distance distance = new Distance(50, Metrics.KILOMETERS);

        //画圆
        Circle circle = new Circle(geoJsonPoint, distance);

        //查询MongoDB数据
        Query query = new Query(Criteria.where("location").withinSphere(circle));
        List<StationLocation> list = mongoTemplate.find(query, StationLocation.class);

        if (CollectionUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        //查询其他需要数据
        //根据查询mongoDB的list集合里面获取站点其他的数据
        //从list获取所有站点id
        //根据所有的站点id获取对应站点数据
        List<Long> stationIdList = list
                .stream()
                .map(StationLocation::getStationId)
                .collect(Collectors.toList());

        //查询其他需要数据，进行封装
        LambdaQueryWrapper<Station> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Station::getId, stationIdList);
        List<Station> stationList = stationService.list(wrapper);

        ArrayList<StationVo> stationVosList = new ArrayList<>();
        //遍历
        stationList.forEach(station -> {
            StationVo stationVo = new StationVo();
            BeanUtils.copyProperties(station, stationVo);

            Long cabinetId = station.getCabinetId();
            Cabinet cabinet = cabinetService.getById(cabinetId);

            if(cabinet.getAvailableNum() > 0) {
                stationVo.setIsUsable("1");
            } else {
                stationVo.setIsUsable("0");
            }
            // 获取空闲插槽数量大于0，可归还
            if (cabinet.getFreeSlots() > 0) {
                stationVo.setIsReturn("1");
            } else {
                stationVo.setIsReturn("0");
            }
            stationVosList.add(stationVo);
        });
        return stationVosList;
    }
}